<?php
include('../../include/functions.inc.php');
if( !isEmpty(getParam('operation')) ) {
    switch( getParam('operation') ) {
        case 'save':
            fntSave($db, $user);
            break;
    }
}

function fntSave($db, $user) {
    
    
    $getID = array();
    $getID = getParam("chkAssign");
    
    foreach ($getID as $ID) {
        
        
        $cargosSql = "SELECT * 
                      FROM cllg_cargo
                      WHERE FK_STUDENT_ASSIGN={$ID}
                      AND (cllg_cargo.TOTAL_PENDIENTE <> 0 OR cllg_cargo.TOTAL_PENDIENTE IS NULL)";
        $cargos = $db->queryToArray($cargosSql);
        
            foreach($cargos as $ca){

                    $strNameMonth = date('M', strtotime(date($ca['DATE_LIMIT'])));
                    #echo $strNameMonth;
                    $intDia = date('j', strtotime(date($ca['DATE_LIMIT'])));
                    #echo $intDia;
                    $intMes = date('n', strtotime(date($ca['DATE_LIMIT'])));
                    #echo $intMes;
                    $intAnio = date('Y', strtotime(date($ca['DATE_LIMIT'])));
                    #echo $intAnio;
                    $strDescripcion = $ca['DESCRIPTION'];
                    #echo $strDescripcion;
                    $intMesesCorrer = intval($_POST['MESES']);
                    #echo "<b>". $intMesesCorrer . "</br>";
                    $intMes += $intMesesCorrer; 
                    if( $intMes > 12 ) {
                        $dif = $intMes - 12;
                        $intMes = ($dif == 0 ? 1 : $dif);
                        $intAnio++;
                    }
                    $strNewNameMonth = date('M', strtotime(date("$intDia-$intMes-$intAnio")));
                    #echo $strNewNameMonth;
                    $strDescripcion = str_replace($strNameMonth, $strNewNameMonth, $strDescripcion);
                    #echo $strDescripcion;
                    $strDateLimit = date('Y-m-d', strtotime(date("$intDia-$intMes-$intAnio")));
                    #echo $strDateLimit;

                    $strQuery ="UPDATE  cllg_cargo 
                                SET     DATE_LIMIT = " . sqlValue($strDateLimit, 'date') . ",
                                        DESCRIPTION = " . sqlValue($strDescripcion, 'text') . ",  
                                        CREATED = " . sqlValue(date('Y-m-d'), 'date') . ",  
                                        CREATED_BY = " . sqlValue($user->name, 'text') . "  
                                WHERE   ID = {$ca['ID']}";
                    #echo $strQuery;
                    $db->query($strQuery);
                    
                }
                #echo $ID['UPDATED_CREATED'];
                $assign = $db->query_select("cllg_student_assign",sprintf("ID=%s",$ID));
                $assign = $assign[0];
                
                $day = date('j', strtotime(date($assign['START_DATE'])));
                $month = date('n', strtotime(date($assign['START_DATE'])));
                $year = date('Y', strtotime(date($assign['START_DATE'])));
                $plusMonths = $_POST['MESES'];
                $month += $plusMonths; 
                if( $month > 12 ) {
                    $dif = $month - 12;
                    $month = ($dif == 0 ? 1 : $dif);
                    $year++;
                    }
                $strStartDate = date('Y-m-d', strtotime(date("$day-$month-$year")));
                $sqlStart = sprintf("UPDATE  cllg_student_assign 
                                       SET     START_DATE = %s
                                       WHERE   ID ='%s'",sqlValue($strStartDate, 'date'), $ID);
                       
                $db->query($sqlStart);
                
         }
         header("Location:index.php");
  }
            

?>